package com.example.musicapi.controller;

import com.example.musicapi.entity.News;
import com.example.musicapi.entity.Remark;
import com.example.musicapi.service.NewsService;
import com.example.musicapi.service.RemarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Base64;
import java.util.List;

@RestController
@RequestMapping("/remark")
public class RemarkController {
    @Autowired
    private RemarkService remarkService;

    @PostMapping("/get")
    @ResponseBody
    public List<Remark> get(@RequestParam(defaultValue = "0") int page,
                               @RequestParam(defaultValue = "300") int size,
                               @RequestParam(required = true) String song_id) {
        return remarkService.search(page, size,song_id);
    }

    @PostMapping("/getAll")
    @ResponseBody
    public List<Remark> getAll(@RequestParam(defaultValue = "0") int page,
                               @RequestParam(defaultValue = "3000") int size,
                               @RequestParam(required = false,defaultValue = "") String key) {
        if(!key.equals(""))
            return remarkService.searchTxt(page, size,key);
        else{
            return remarkService.findAll(page, size);
        }
    }

    @PostMapping("/add")
    public String add(String song_id, String user,@RequestParam(required = false) String avatarBase64 ,
                      String txt, String rate,String like_num) {
        Remark u=new Remark();
        if(avatarBase64!=null && !avatarBase64.equals("")){
            byte[] avatarBytes = Base64.getDecoder().decode(avatarBase64);
            u=new Remark(null,song_id,user,avatarBytes,txt,rate,like_num);
        }else{
            return "fail";
        }
        u=remarkService.add(u);
        if(u!=null && u.getId()!=null)
            return "success";
        else{
            return "fail";
        }

    }

    @PostMapping("/update")
    public String update(String id, String like_num) {
        try{
            remarkService.update(id,like_num);
            return "success";
        }catch (Exception e){
            return "fail";
        }

    }
    @PostMapping("/delete")
    public String delete(String id) {
        try {
            remarkService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
        return "success";
    }

}

